/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author khanhlv_b00554
 */
public class Test extends BaseDAO {

    private Connection con = getConnection();
    private ResultSet rs = null;
    private PreparedStatement pStmt = null;
    private Statement stmt = null;
    private String TestName;
    private String TestTime;
    private String Status;
    private String SubID;

    public String getSubID() {
        return SubID;
    }

    public void setSubID(String SubID) {
        this.SubID = SubID;
    }
    
    public String getStatus() {
        return Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }

    public String getTestName() {
        return TestName;
    }

    public void setTestName(String TestName) {
        this.TestName = TestName;
    }

    public String getTestTime() {
        return TestTime;
    }

    public void setTestTime(String TestTime) {
        this.TestTime = TestTime;
    }

    public Test() {
    }

    public ResultSet viewSubSubject() {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM SUBSUBJECT");

        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public ResultSet viewTest() {

        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Test T,SUBSUBJECT S WHERE T.SUBID = S.SUBID");
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public ResultSet viewTestSub(int id) {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Test T,SUBSUBJECT S WHERE T.SUBID = S.SUBID AND T.SUBID = "+id);
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public boolean deleteTest(int id) {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Result WHERE TestID = " + id);
            if(rs.next()) {
                return false;
            } else {
                pStmt = con.prepareCall("DELETE FROM TEST WHERE TestID = ?");
                pStmt.setInt(1, id);
                if(pStmt.executeUpdate() == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean updateTest(int id){
        try {
            pStmt = con.prepareCall("Update Test Set TestName = ?,TestTime = ?,Status = ? WHERE TestID = ?");
            pStmt.setString(1, this.getTestName());
            pStmt.setString(2, this.getTestTime());
            pStmt.setString(3, this.getStatus());
            pStmt.setInt(4, id);
            if (pStmt.executeUpdate() == 1) {
                return true;
            } else {
                return false;
            }

        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean updateTestCheck(int id) {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Result WHERE TestID = " + id);
            if(rs.next()) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
    public ResultSet viewTestCheck(int id) {

        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Test WHERE TestID = "+id);
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public ResultSet viewTestID(String id) {
        int subSubject = Integer.parseInt(id);
        if(subSubject == 0) {
            this.viewTest();
        } else {
            this.viewTestSub(subSubject);
        }
        return rs;
    }
    public boolean insertTest() {
        //int time = Integer.parseInt(this.getTestTime());
        //boolean status = Boolean.parseBoolean(this.getStatus());
        try {
            pStmt = con.prepareCall("INSERT INTO TEST VALUES (?,?,?,?)");
            pStmt.setString(1, this.getSubID());
            pStmt.setString(2, this.getTestName());
            pStmt.setString(3, this.getTestTime());
            pStmt.setString(4, this.getStatus());

            if(pStmt.executeUpdate() == 1)  {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
